import BelongSelect from '@/components/BelongSelect';
import { AntdModalProps } from '@/hooks/useAntdModal';
import { saveCertificate } from '@/service/globalSetting';
import { Checkbox, Form, Input, Modal, Radio, Select } from 'antd';
import React from 'react';
const SaveCertificate = ({ form, refresh, closeModal, modalProps }: AntdModalProps) => {
  const onOk = async () => {
    let data = await form.validateFields()
    data.default = data.default ? 0 : 1
    await saveCertificate(data)
    refresh()
    closeModal()
  }
  return (<Modal title="账号类型" {...modalProps} onOk={onOk}>
    <Form form={form} labelCol={{ span: 4 }}
      initialValues={{
        authMode: 'account'
      }}
    >
       <Form.Item name='belong' label='项目归属'>
        <BelongSelect/> 
      </Form.Item>
      <Form.Item hidden name='id'>
        <Input />
      </Form.Item>
      <Form.Item label="账号类型" name='type' rules={[{
        required: true,
        message: '账号类型'
      }]}>
        <Select placeholder="请选择" options={[{
          value: 'Npm',
          label: 'Npm'
        }, {
          value: 'Jenkins',
          label: 'Jenkins'
        }, {
          value: 'git',
          label: '代码库'
        }]} />
      </Form.Item>

      <Form.Item label="名称" name='title' rules={[{
        required: true,
        message: '账号类型'
      }]}>
        <Input placeholder='名称' />
      </Form.Item>
      <Form.Item label="服务地址" name='serviceUrl' rules={[{
        required: true,
        message: '服务地址'
      }]}>
        <Input placeholder='服务地址' />
      </Form.Item>
      <Form.Item label="认证方式" name='authMode' required>
        <Radio.Group>
          <Radio value="account">账号</Radio>
          <Radio value="token">token</Radio>
          <Radio value="ssh">SSH 密钥</Radio>

        </Radio.Group>
      </Form.Item>
      <Form.Item noStyle shouldUpdate={(a, b) => a.authMode != b.authMode}>
        {({ getFieldValue }) => {
          if (getFieldValue('authMode') === 'account') {
            return <>
              <Form.Item label="用户名" name='username' rules={[{
                required: true,
                message: '用户名'
              }]}>
                <Input />
              </Form.Item>
              <Form.Item label="密码" name='password' rules={[{
                required: true,
                message: '密码'
              }]} >
                <Input.Password />
              </Form.Item>
            </>
          }
          if (getFieldValue('authMode') === 'token') {
            return <Form.Item label="token" name='token' rules={[{
              required: true,
              message: 'token'
            }]}>
              <Input.Password />
            </Form.Item>
          }
          if (getFieldValue('authMode') === 'ssh') {
            return <>
              <Form.Item label="公钥" name='publicKey' rules={[{
                required: true,
                message: '公钥'
              }]}>
                <Input.TextArea rows={3} />
              </Form.Item>
              <Form.Item label="私钥" name='privateKey' rules={[{
                required: true,
                message: '私钥'
              }]}>
                <Input.TextArea rows={3} />
              </Form.Item>
            </>
          }

          return null
        }}
      </Form.Item>



      <Form.Item label="是否默认" name='default' >
        <Checkbox />
      </Form.Item>
    </Form>
  </Modal>)
}
export default SaveCertificate